package com.edgar.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Edgar Zhang
 * 
 */
public class ContextParamsServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 7781297625417301023L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("test init parameters<br>");
        Enumeration e = getServletConfig().getInitParameterNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            out.println("param name = " + key + "<br>");
        }
        out.println("main email is "
                + getServletConfig().getInitParameter("mainEmail"));
        out.println("<br>");
        out.println("admin email is "
                + getServletConfig().getInitParameter("adminEmail"));

        out.println("<br />test context parameters<br>");
        e = getServletContext().getInitParameterNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            out.println("param name = " + key + "<br>");
        }
        out.println("appEmail1 is "
                + getServletContext().getInitParameter("appEmail1"));
        out.println("<br>");
        out.println("appEmail2 is "
                + getServletContext().getInitParameter("appEmail2"));

        System.out.println(getServletContext().getServerInfo());
    }
}
